<!DOCTYPE html>
<html class="x-admin-sm">
<head>
<meta charset="UTF-8">
<title>列表</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="/css/font.css">
<link rel="stylesheet" href="/css/index.css">
<script type="text/javascript" src="/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="/js/index.js"></script>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/layui-post.js"></script>
</head>
<body>
<div class="x-nav">
	<span class="layui-breadcrumb">
	  <a href="">用户管理</a><a><cite>用户订单</cite></a>
	</span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
		                <div class="layui-inline layui-show-xs-block">
		                    <input type="text" id="orderNo" autocomplete="off" value="" placeholder="请输入订单编码" class="layui-input">
		                </div>
                        <div class="layui-inline layui-show-xs-block">
                            <select name="userId" id="userId" lay-search lay-verify="required"><option value="">请选择用户</option></select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <select name="status" id="status" lay-search lay-verify="required">
                                <option value="">请选择支付状态</option>
                                <option value="0">未支付</option>
                                <option value="1">已支付</option>
                                <option value="2">已取消</option>
                                <option value="3">已退款</option>
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <select name="isSure" id="isSure" lay-search lay-verify="required">
                                <option value="">请选择确认状态</option>
                                <option value="0">待确认</option>
                                <option value="1">已确认</option>
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <select name="orderStatus" id="orderStatus" lay-search lay-verify="required"><option value="">请选择订单状态</option></select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <select name="sendType" id="sendType" lay-search lay-verify="required"><option value="">请选择配送类型</option></select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn"  lay-submit="" lay-filter="sreach" id="search"><i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-header">
<!--                    <button class="layui-btn" onclick="xadmin.open('新增','./cateringorderadd')"><i class="layui-icon layui-icon-add-circle"></i>添加</button>-->
                </div>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form" id="table" lay-filter="table"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="ImportExcel"></div>
</body>
<script>
$.ajaxSetup({contentType: "application/json; charset=utf-8"});
var enusData = [];
layui.use(['table','form'], function(){
	var table = layui.table
    $.post(geturl()+'/cateringUser/select?token='+sessionStorage.getItem("token"), JSON.stringify({}), function (res) {
        for(var i in res.data) $('#userId').append('<option value="'+res.data[i].userId + '">' + res.data[i].nickName + '</option>');
        layui.form.render();
    })
    $.post(geturl()+'/enums/select?token='+sessionStorage.getItem("token"), JSON.stringify({parentEnumCode:"DELIVERY_TYPE<->ORDER_STATUS",orderByAscType:"enumCode"}), function (res) {
        for(var key in res.data.DELIVERY_TYPE) $('#sendType').append('<option value="'+key + '">' + res.data.DELIVERY_TYPE[key] + '</option>');
        for(var key in res.data.ORDER_STATUS) $('#orderStatus').append('<option value="'+key + '">' + res.data.ORDER_STATUS[key] + '</option>');
        layui.form.render();
        enusData = res.data;
    }).done(function(res){
        table.render({
            elem: 'table', //指定元素
            url:geturl()+'/cateringOrder/select?token='+sessionStorage.getItem("token"),
            contentType:'application/json',
            method:'post',
            page:true,height: $(window).height()-210,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            where:{orderByType: "createTime"},
            cols: [[
                //{type: 'checkbox',width: 80},
                {title: '序号',align:"center",halign:"center",width:80,type: 'numbers'},
                {field:'orderNo',align:"center",width:100,title:'订单确认',templet:onloadOrderStatus},
                {field:'orderNo',align:"center",width:120,title:'订单编号',templet:onloadDetail},
                {field:'userName',align:"center",width:120,title:'用户账号'},
                {field:'sendType',align:"center",title:'配送类型',templet:function(d){ if(d.sendType == 1) return "外卖配送";else return "到店自取";}},
                {field:'sendTime',align:"center",title:'配送时间'},
                {field:'payMoney',align:"center",title:'实际支付'},
                {field:'useIntegra',align:"center",title:'使用积分'},
                {field:'sendIntegra',align:"center",title:'赠送积分',},
                {field:'status',align:"center",title:'支付状态',templet:function(d){ if(d.status == 0)return "待支付";else if(d.status == 2)return "已取消";else if(d.status == 3)return "已退款";else return "已支付";}},
                {field:'orderStatus',align:"center",title:'订单状态',templet:onloadOrderLog},
                {field:'isDel',align:"center",title:'用户删除',templet:function(d){ if(d.isDel == 0)return "否";else return "是";}},
                {field:'createTime',align:"center",title: '下单时间',width: 150,},
                {field:'orderNo',align:"center",width:100,title:'是否退款',templet:onloadStatus},
                //{fixed:'right',align: 'center',title:'操作',width: 100,templet: onloadTool }
            ]],
        });
    })
    layui.form.on('switch(switchTest)', function(data){
        var id = data.elem.attributes['data_id'].nodeValue;
        var status = data.elem.attributes['status'].nodeValue;
        var em = $(data.othis[0]);
        if(status == 0) {
            closeSwitch1(data,status,em);layer.msg("订单未支付，不可退款",{icon:0,time:1000})
        }else if(status == 2) {
            closeSwitch1(data,status,em);layer.msg("订单已取消，不可退款",{icon:0,time:1000})
        }else{
            layer.confirm('确认'+(status == 1 ? '' : '取消')+'退款吗？',{icon: 0,title:'提示',cancel:function(){closeSwitch1(data,status,em);}},function(index){
                layuiPost(geturl()+'/cateringOrder/updateStatus?token='+sessionStorage.getItem("token"),{status:3,sysId:id,orderStatus:'ORDER_STATUS_99'});
            },function(index){
                closeSwitch1(data,status,em)
            });
        }
    });
    layui.form.on('switch(switchOrderStatus)', function(data){
        var id = data.elem.attributes['data_id'].nodeValue;
        var status = data.elem.attributes['status'].nodeValue;
        var sendType = data.elem.attributes['sendType'].nodeValue;
        var em = $(data.othis[0]);
        layer.confirm('确认'+(status == 0 ? '' : '取消')+'接受订单吗？',{icon: 0,title:'提示',cancel:function(){closeSwitchOrder(data,status,em);}},function(index){
            var orderStatus;
            if(sendType == 1) orderStatus = 'ORDER_STATUS_03,ORDER_STATUS_04,ORDER_STATUS_21';
            else orderStatus = 'ORDER_STATUS_03,ORDER_STATUS_04,ORDER_STATUS_22'
            layuiPost(geturl()+'/cateringOrder/updateStatus?token='+sessionStorage.getItem("token"),{isSure:1,orderStatus:orderStatus,sysId:id});
        },function(index){
            closeSwitchOrder(data,status,em)
        });
    });
});
function enums(s,enumCode){
    for(var key in enusData[enumCode]) if(key === s) return enusData[enumCode][s];
}
function onloadStatus(data){
    if(data.status == 3){
        return '<input type="checkbox" checked="" name="switch" lay-skin="switch" disabled data_id="'+data.sysId+'" status="'+data.status+'" lay-filter="switchTest" lay-text="是|否">'
    }else {
        return '<input type="checkbox" name="switch" lay-skin="switch" data_id="'+data.sysId+'" status="'+data.status+'" lay-filter="switchTest" lay-text="是|否">'
    }
}
function onloadOrderStatus(data){
    if(data.status == 1){
        if(data.isSure == 0){
            return '<input type="checkbox" name="switch" lay-skin="switch" data_id="'+data.sysId+'" status="'+data.isSure+'" sendType="'+data.sendType+'" lay-filter="switchOrderStatus" lay-text="是|否">'
        }else {
            return '<input type="checkbox" checked="" name="switch" lay-skin="switch" disabled data_id="'+data.sysId+'" status="'+data.isSure+'" sendType="'+data.sendType+'" lay-filter="switchOrderStatus" lay-text="是|否">'
        }
    }else if(data.status == 2){
        return "订单已取消"
    }else if(data.status == 3){
        return "订单已退款"
    }else if(data.status == 0){
        return "<span style='color: red'>订单待支付</span>"
    }
}
/*删除*/
function member_del(obj,id){
    layer.confirm('确认要删除吗？',function(index){
    	layuiPost(geturl()+'/cateringOrder/delete?token='+sessionStorage.getItem("token"),{sysId:id});
    });
}

$('#search').on('click', function() {
	layui.table.reload('table', {
		page: {curr: 1},
		where : {
            orderByType: "createTime",
            orderNo:$("#orderNo").val(),
            userId:$("#userId").val(),
            orderStatus:$("#orderStatus").val(),
            status:$("#status").val(),
            isSure:$("#isSure").val(),
            sendType:$("#sendType").val(),
		},
	});
	return false;
});
function closeSwitch1(data,status,em){
    if(status != 3){
        data.othis[0].classList.remove('layui-form-onswitch');
        em.children('em').text('否');
    }else{
        data.othis[0].classList.add('layui-form-onswitch');
        em.children('em').text('是');
    }
}
function closeSwitchOrder(data,status,em){
    if(status == 0){
        data.othis[0].classList.remove('layui-form-onswitch');
        em.children('em').text('否');
    }else{
        data.othis[0].classList.add('layui-form-onswitch');
        em.children('em').text('是');
    }
}
function onloadTool(data) {
    return '<a title="编辑" onclick="xadmin.open(\'编辑\',\'cateringorderedit?sysId=' + data.sysId + '\')" href="javascript:;"><i class="layui-icon">&#xe642;</i></a>' +
        '<a title="删除" onclick="member_del(this,\'' + data.sysId + '\')" href="javascript:;"><i class="layui-icon">&#xe640;</i></a>';
}

function onloadDetail(data) {
    return '<a title="详情" style="color: blue;text-decoration: underline;" onclick="xadmin.open(\'详情\',\'cateringordercart?sysId=' + data.sysId + '\')" href="javascript:;">'+data.orderNo+'</a>'
}
function onloadOrderLog(data) {
    return '<a title="物流跟踪" style="color: blue;text-decoration: underline;" onclick="xadmin.open(\'详情\',\'cateringorderlog?sysId=' + data.sysId + '\')" href="javascript:;">'+enums(data.orderStatus,"ORDER_STATUS")+'</a>'
}
</script>
</html>